﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Modelling;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.ShprossesPropertyGrid.ComponentsProxy;
using System.ComponentModel;

namespace ITAP.ShprossesBuilder.ShprossesPropertyGrid
{
    public partial class ShprossesGrid : ITAP.Core.CustomPropertyGrid.CustomPropertyGrid, IModelVisitor
    {
        public ShprossesGrid() : base()
        {
            InitializeComponent();
            allowTypesToDisplay = new List<Type>();
        }

        public ShprossesGrid(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        private ITAP.ShprossesBuilder.MainControl mainControl;

        public MainControl MainControl
        {
            get
            { return mainControl; }
            set
            { mainControl = value; }
        }

        private List<Type> allowTypesToDisplay;
        /// <summary>
        /// Порядок задания в списке типов элементов определяет приоритет, с которым они будут отображаться в 
        /// PropertyGrid. Например, если нулевой элемент - typeof(ModelCell), а первый - typeof(ShprossConnector), 
        /// то при щелчке по пустому пространству ModelCell отобразятся свойства ModelCell, но если щёлкнуть по 
        /// ShprossConnector, то несмотря на то, что выделен и ModelCell, отобразятся свойства ShprossConnector.
        /// </summary>
        public List<Type> AllowTypesToDisplay
        {
            get
            { return allowTypesToDisplay; }
            set
            { allowTypesToDisplay = value; }
        }

        ///         #region IModelVisitor Members
        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Model model)
        {
            //    throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Connector connector)
        {
            //    throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Shpross shpross)
        {
            //    throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Spacer spacer)
        {
            //    throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ShprossConnectorStub stub)
        {
            //    throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ShprossConnector shprossConnector)
        {
            //    throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.SpacerElement spacerElement)
        {
            //    throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.SpacerConnector spacerConnector)
        {
            //    throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ModelCell modelCell)
        {
            //     throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ModelContainer modelContainer)
        {
            ITAP.ShprossesBuilder.Modelling.Components.IComponent selectedComponent = null;
            List<ITAP.ShprossesBuilder.Modelling.Components.IComponent> selectedComponents = new List<ITAP.ShprossesBuilder.Modelling.Components.IComponent>();
            foreach (ITAP.ShprossesBuilder.Modelling.Components.IComponent innerComponent in modelContainer.AllInnerComponents)
                if (innerComponent.Selected && allowTypesToDisplay.Contains(innerComponent.GetType()))
                    selectedComponents.Add(innerComponent);

            if (modelContainer.Selected)
                selectedComponents.Add(modelContainer);

            int index = -1;
            foreach (ITAP.ShprossesBuilder.Modelling.Components.IComponent _selected in selectedComponents)
            {
                int currentIndex = this.allowTypesToDisplay.IndexOf(_selected.GetType());
                if (currentIndex > index)
                {
                    index = currentIndex;
                    selectedComponent = _selected;
                }
            }
            //            selectedComponent = selectedComponents[0];

            if (selectedComponent != null)
            {
                CreateComponentProxy proxy = new CreateComponentProxy(this.MainControl);
                selectedComponent.AcceptVisitor(proxy);

                this.SelectedObject = proxy.ProxyObject;
            }
            else
                this.SelectedObject = null;
        }

        public void Visit(StickConnector stickConnector)
        {
            throw new NotImplementedException();
        }

        public void Visit(SnapOnCap snapOnCap)
        {
            throw new NotImplementedException();
        }

        public void Visit(ShprossEndToEndConnector endToEndConnector)
        {
            throw new NotImplementedException();
        }

        public void Visit(ShprossChain shprossChain)
        {
            //      throw new NotImplementedException();
        }

        public void Visit(SpacerContainer spacerContainer)
        {
            //throw new NotImplementedException();
        }

        public void Visit(CrossConnector crossConnector)
        {
            //      throw new NotImplementedException();
        }
    }
}
